package com.yixun.qfbao.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.yixun.qfbao.mapper.ModelMapper;
import com.yixun.qfbao.model.Model;
import com.yixun.qfbao.model.enums.DisplayStatusEnum;
import com.yixun.qfbao.model.enums.ModelDisplayTypeEnum;
import com.yixun.qfbao.model.enums.ModelTypeEnum2;
import com.yixun.qfbao.model.enums.ReportModelShowStatusEnum;
import com.yixun.qfbao.model.enums.RequiredStatusEnum;
import com.yixun.qfbao.service.ModelService;

@Service("modelService")
public class ModelServiceImpl implements ModelService {
	
	@Autowired
	private ModelMapper mapper;
	

	/**
	 * 查询模板集合
	 */
	@Override
	public List<Model> selectListByDto(Model model) {
		List<Model> modelList = mapper.selectListByDto(model);
		if(!CollectionUtils.isEmpty(modelList)) {
			return modelList;
		}
		if(!ModelTypeEnum2.PARTNER_CHECK.getNo().equals(model.getType())
				&& !ModelTypeEnum2.COMPANY.getNo().equals(model.getType())) {
			return null;
		}
		// 如果查询类型是人员审核
		synchronized (this) {
			List<Model> modelList2 = mapper.selectListByDto(model);
			if(!CollectionUtils.isEmpty(modelList2)) {
				return modelList2;
			}
			// 因为ID的原因，插入之后进行再次查询
			if(insertPartnerCheckBaseModel(model.getCreateUserId(), 
					model.getCompanyId(), model.getType()) > 0) {
				return mapper.selectListByDto(model);
			}
			return null;
		}
	}
	
	/**
	 * 批量插入基础模板
	 * @param userId
	 * @param companyId
	 * @return
	 */
	private int insertPartnerCheckBaseModel(String userId, long companyId, String type) {
		List<Model> modelList = getBaseModelList(userId, companyId, type);
		return mapper.batchInsert(modelList);
	}
	
	/**
	 *  如果没有查询到，则先添加
	 * @param userId
	 * @return
	 */
	private List<Model> getBaseModelList(String userId, long companyId, String type) {
		List<Model> modelList = new ArrayList<>();
		if(ModelTypeEnum2.PARTNER_CHECK.getNo().equals(type)) {
			Model baseModel1 = new Model();
			baseModel1.setName("身份证号码");
			baseModel1.setType(ModelTypeEnum2.PARTNER_CHECK.getNo());
			baseModel1.setDisplayType(ModelDisplayTypeEnum.TEXT.getNo());
			baseModel1.setDisplayStatus(DisplayStatusEnum.hide.getCode());
			baseModel1.setRequiredStatus(RequiredStatusEnum.required.getCode());
			baseModel1.setCreateUserId(userId);
			baseModel1.setCompanyId(companyId);
			modelList.add(baseModel1);
			
			Model baseModel2 = new Model();
			baseModel2.setName("经纪人服务卡");
			baseModel2.setType(ModelTypeEnum2.PARTNER_CHECK.getNo());
			baseModel2.setDisplayType(ModelDisplayTypeEnum.IMAGE.getNo());
			baseModel2.setDisplayStatus(DisplayStatusEnum.hide.getCode());
			baseModel2.setRequiredStatus(RequiredStatusEnum.required.getCode());
			baseModel2.setCreateUserId(userId);
			baseModel2.setCompanyId(companyId);
			modelList.add(baseModel2);
		}else if(ModelTypeEnum2.COMPANY.getNo().equals(type)) {
			Model baseModel1 = new Model();
			baseModel1.setName("营业执照");
			baseModel1.setType(type);
			baseModel1.setDisplayType(ModelDisplayTypeEnum.IMAGE.getNo());
			baseModel1.setDisplayStatus(DisplayStatusEnum.hide.getCode());
			baseModel1.setRequiredStatus(RequiredStatusEnum.no_required.getCode());
			baseModel1.setCreateUserId(userId);
			baseModel1.setCompanyId(companyId);
			modelList.add(baseModel1);
		}
		
		return modelList;
	}

	/**
	 *     重新添加模板集合。 先把原先类型删除，后新增. 
	 *     	传入空集合的话即直接删除所有模板
	 * @param modelList
	 * @return
	 */
	@Override
	@Transactional
	public int reAddModel(Long companyId, String userId, List<Model> modelList) {
		if(CollectionUtils.isEmpty(modelList)) {
			return 0;
		}
		Model model = new Model();
		model.setType(modelList.get(0).getType());
		model.setCompanyId(companyId);
		mapper.dels(model);
		
		if(CollectionUtils.isEmpty(modelList)) {
			return 0;
		}
		
		for (Model eachModel : modelList) {
			eachModel.setCompanyId(companyId);
			eachModel.setCreateUserId(userId);
		}
		
		return mapper.batchInsert(modelList);
	}
	
}
